<template>
  <a-modal
    title="编辑"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item v-show="false"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['materialPoolId']"/>
        </a-form-item>

        <a-form-item v-show="false"
          label="主键"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入主键" v-decorator="['materialPoolId', {rules: [{required: true, message: '请输入主键！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="物料ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入物料ID" v-decorator="['materialId', {rules: [{required: true, message: '请输入物料ID！'}]}]" />
        </a-form-item>

        <a-form-item
          label="物料名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入物料名称" v-decorator="['materialName', {rules: [{required: true, message: '请输入物料名称！'}]}]"
                   disabled/>
        </a-form-item>

        <a-form-item
          label="料号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入料号" v-decorator="['materialCode', {rules: [{required: true, message: '请输入料号！'}]}]"
                   disabled/>
        </a-form-item>

        <a-form-item v-show="true"
          label="出库数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input-number placeholder="请输入数量" v-decorator="['number', {rules: [{required: true, message: '请输入数量！'}]}]" :min="0"/>
        </a-form-item>

        <a-form-item v-show="false"
          label="物料类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入物料类型" v-decorator="['materialType', {rules: [{required: true, message: '请输入物料类型！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="池类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入池类型" v-decorator="['poolType', {rules: [{required: true, message: '请输入池类型！'}]}]" />
        </a-form-item>

        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入备注" v-decorator="['remark', {rules: [{required: true, message: '请输入备注！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="状态（字典 0正常 1冻结 2删除）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入状态（字典 0正常 1冻结 2删除）" v-decorator="['status', {rules: [{required: true, message: '请输入状态（字典 0正常 1冻结 2删除）！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="创建时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入创建时间" v-decorator="['createTime', {rules: [{required: false, message: '请输入创建时间！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="创建人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入创建人" v-decorator="['createUser', {rules: [{required: false, message: '请输入创建人！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="更新时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入更新时间" v-decorator="['updateTime', {rules: [{required: false, message: '请输入更新时间！'}]}]" />
        </a-form-item>

        <a-form-item v-show="false"
          label="更新人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入更新人" v-decorator="['updateUser', {rules: [{required: false, message: '请输入更新人！'}]}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/emsmaterialpool/emsMaterialPoolManage'

  export default {
    data() {
      return {
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 15}
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      edit(record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(record);
        }, 100)
      },
      handleSubmit() {
        const {form: {validateFields}} = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields();
        this.visible = false
      }
    }
  }
</script>
